package com.study.system.controller;

import com.study.system.dto.ResultData;
import com.study.system.entity.Activity;
import com.study.system.entity.Club;
import com.study.system.enums.ResultCode;
import com.study.system.service.ActivityService;
import com.study.system.utils.RequestUtil;
import com.study.system.vo.ActivityInfoVO;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 查看"我参加的所有活动"
     *
     * @param request 前端发送的请求
     * @return 查询结果
     */
    @GetMapping("/already_attend")
    public ResultData<List<ActivityInfoVO>> findAllActivityByUserId(HttpServletRequest request) {
        Integer userId = RequestUtil.getUserId(request);
        List<ActivityInfoVO> activities = activityService.findAllActivityByUserID(userId);
        return ResultData.success(activities);
    }

    /**
     * 查看“可参加的活动”
     *
     * @param request 前端发送的请求
     * @return 查询结果
     */
    @GetMapping("/could_attend")
    public ResultData<List<ActivityInfoVO>> findAllActivityCanAttend(HttpServletRequest request) {
        Integer userId = RequestUtil.getUserId(request);
        List<ActivityInfoVO> activities = activityService.findAllActivityCanAttend(userId);
        return ResultData.success(activities);
    }

    /**
     * 展示社团活动功能
     *
     * @param clubInfo 社团信息（只需要社团ID）
     * @return 查询结果
     */
    @PostMapping("/show_club_all_activity")
    public ResultData<List<ActivityInfoVO>> findAllClubActivity(@RequestBody Club clubInfo) {
        List<ActivityInfoVO> result = activityService.findAllClubActivity(clubInfo.getId());
        return ResultData.success(result);
    }

    /**
     * 创建活动功能
     *
     * @param request  前端发送的请求（用户ID）
     * @param activity 社团信息
     * @return 操作是否成功
     */
    @PostMapping("/create_activity")
    public ResultData<Boolean> insertActivity(HttpServletRequest request, @RequestBody Activity activity) {
        Integer creatorId = RequestUtil.getUserId(request);
        Boolean result = activityService.insertActivity(creatorId, activity);
        if (result) {
            return ResultData.success(true);
        }
        return ResultData.fail(ResultCode.DATABASE_ERROR);
    }

    /**
     * 修改活动状态
     *
     * @param updateInfo 活动信息（活动ID、新的活动状态）
     * @return 操作是否成功
     */
    @PostMapping("/update_activity_status")
    public ResultData<Boolean> updateActivityStatus(@RequestBody Map<String, Integer> updateInfo) {
        Integer activityInteger = updateInfo.get("activityId");
        Integer newStatus = updateInfo.get("newStatus");
        return ResultData.success(activityService.updateActivityStatus(activityInteger, newStatus));
    }

    // 修改活动信息
    @PostMapping("/modify_activity_info")
    public ResultData<Boolean> modifyActivityInfo(@RequestBody ActivityInfoVO activityInfo) {
        return ResultData.success(activityService.modifyActivityInfo(activityInfo));
    }

    /**
     * 参加活动
     * 
     * @param request      请求头，需要token
     * @param activityInfo 活动id
     * @return 操作是否成功
     */
    @PostMapping("/attend_activity")
    public ResultData<Boolean> attendActivity(HttpServletRequest request, @RequestBody Map<String, Integer> activityInfo) {
        Integer userId = RequestUtil.getUserId(request);
        Integer activityId = activityInfo.get("activityId");
        Boolean result = activityService.userAttendActivity(userId, activityId);
        return ResultData.success(result);
    }

    /**
     * 用户退出活动
     *
     * @param request 请求头
     * @param activityInfo 活动信息
     * @return 操作结果
     */
    @PostMapping("/user_exit_activity")
    public ResultData<Boolean> userExitActivity(HttpServletRequest request, @RequestBody Map<String, Integer> activityInfo) {
        Integer userId = RequestUtil.getUserId(request);
        Integer clubId = activityInfo.get("activityId");
        activityService.userExitActivity(userId, clubId);
        return ResultData.success(true);
    }

    // 普通用户按关键字搜索已参加的活动
    @PostMapping("/searchActivityByWord")
    public ResultData<List<ActivityInfoVO>> searchActivityByWord(HttpServletRequest request, @RequestBody Map<String, String> word) {
        Integer userId = RequestUtil.getUserId(request);
        return ResultData.success(activityService.searchActivityByWord(userId, word));
    }

    // 普通用户按关键字搜索可以参加的活动
    @PostMapping("/searchCouldAttendActivityByWord")
    public ResultData<List<ActivityInfoVO>> searchCouldAttendActivityByWord(HttpServletRequest request, @RequestBody Map<String, String> word) {
        Integer userId = RequestUtil.getUserId(request);
        return ResultData.success(activityService.searchCouldAttendActivityByWord(userId, word));
    }

    @PostMapping("/searchCurClubActivityByWord")
    public ResultData<List<ActivityInfoVO>> searchCurClubActivityByWord(@RequestBody Map<String, String> word) {
        return ResultData.success(activityService.searchCurActivityByWord(word));
    }


}
